//index.js
//获取应用实例
const app = getApp()
var amapFile = require('../../utils/amap-wx.js');
import { queryIndexData } from '../../http/api.js';
Page({
  data: {
    address: '',//展示地址
    carouselList: [],//轮播图
    businessDetailsList:[],//商家列表
    advertisingList:[],//广告图
    indicator: true, //是否显示指示点
    interval: 3000,  //自动切换时间间隔
    duration: 400,  //滑动动画时长
    autoplay: true, //是否自动切换
    circular: true, //是否采用衔接滑动
  },
  onShow: function () {
    this.getUserSetting();
  },
  onLoad () {
    wx.showShareMenu({
      withShareTicket: true, //要求小程序返回分享目标信息
      menus:['shareAppMessage','shareTimeline']
    })
  },
  onShareAppMessage: function () {
    return {
      title: '【云雀到家】 生活购物便捷服务，我们是专业的！',
      path: '/pages/shop/home/index',
      success: function (res) {

      }
    }
  },
  onShareTimeline: function (){
    return {
      title: '【云雀到家】 生活购物便捷服务，我们是专业的！',
      path: '/pages/shop/home/index',
      success: function (res) {

      }
    }
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  //获取设置信息
  getUserSetting: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
                vm.getUserSetting();
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getUserLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else {
          //调用wx.getLocation的API
          vm.getUserLocation();
        }
      }

    });
  },
  //获取经纬度
  getUserLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        let latitude = res.latitude;
        let longitude = res.longitude;
        vm.getUserLocal(latitude, longitude);
      },
      fail: function () {
        vm.getUserSetting();
      }
    })
  },
  //获取城市
  getUserLocal: function (latitude, longitude) {
    let vm = this;
   var myAmapFun = new amapFile.AMapWX({ key: '182fded5b7c72add5fff27e43903bdb8' });
    myAmapFun.getRegeo({
      success: function (res) {
        //获取并保存省市区
        let provinceName = res[0].regeocodeData.addressComponent.province;
        let cityName = res[0].regeocodeData.addressComponent.city;
        let districtName = res[0].regeocodeData.addressComponent.district;
        wx.setStorageSync('locationState', {provinceName,cityName,districtName,latitude,longitude});
        //地址展示
        let address = `${cityName}${districtName}`;
        vm.setData({address});
        //拉取首页数据
        vm.queryIndexData(districtName,latitude,longitude);
        // vm.queryIndexData('北京市','市区','海淀区',39.982867,116.321675);
      },
      fail: function (err) {
        //失败回调
        console.log(err)
      }
    })
  },
  //查询首页数据
  queryIndexData: function(districtName,latitude,longitude){
    let obj = {districtName,latitude,longitude,maxCarousel:5,maxAdvertising:3,current:1,size:10};
    let vm = this;
    queryIndexData(obj).then(res=>{
      if(res.code === 200){
        let advertisingList = res.data.advertisingList;
        let businessDetailsList = res.data.businessDetailsList;
        let carouselList = res.data.carouselList;
        vm.setData({
          advertisingList,
          businessDetailsList,
          carouselList
        });
      }
    });
  },
  //进入店铺
  goShop: function(e){
    let businessId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/shop/index?businessId=${businessId}`,
    })
  }
})
